package com.kangyi.user.controller;

import com.kangyi.result.Result;
import com.kangyi.user.service.RegionService;
import com.kangyi.vo.RegionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/user/region")
@Api(tags = "地区相关接口")
@Slf4j
public class RegionController {

    @Autowired
    private RegionService regionService;

    @GetMapping("/inquiry")
    @ApiOperation("查询所有地区")
    @Cacheable(cacheNames = "region_", key = "'userInquiry_'")
    public Result<List<RegionVO>> inquiryRegion(){
        log.info("查询所有地区");
        try {
            List<RegionVO> regionVOList = regionService.inquiryRegion();
            return Result.success(regionVOList,"查询成功！");
        } catch (Exception e) {
            return Result.error("查询失败！"+e.getMessage());
        }
    }


    @GetMapping("/cities")
    @ApiOperation("查询所有市")
    @Cacheable(cacheNames = "region_", key = "'userGet_'")
    public Result<List<String>> getAllCities() {
        try {
            List<String> cities = regionService.getAllCities();
            return Result.success(cities, "查询成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/districts")
    @ApiOperation("根据市查询所有区")
    @Cacheable(cacheNames = "region_", key = "'userGetCity_'+#city")
    public Result<List<RegionVO>> getDistrictsByCity(@RequestParam String city) {
        try {
            List<RegionVO> districts = regionService.getDistrictsByCity(city);
            return Result.success(districts, "查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
